<?php

class EmailController extends Zend_Controller_Action
{

    function init()
    {

    }

    function indexAction()
    {        
        $this->_helper->layout->disableLayout(true);
        $this->_helper->viewRenderer->setNoRender(true);
        //default return
        $response = Fpt_Server::setOutputData(0, '', '');
        /* -----------------------------------------------------------------
           Get param sent to job send mail
        */
        
        try {
            
            // Include config send mail
            include(APPLICATION_PATH.'/configs/mail.php');

            if (is_array($configsender) && !empty ($configsender) )
            {
                
                // Get Url to send
                $url_send = $this->_request->getParam('url', '');
                
                // Get name of the sender
                $sender_name = $this->_request->getParam('sender_name', '');

                if (empty ($sender_name)) {                    
                    return false;
                }

                // Get user email
                $email_user = $this->_request->getParam('email_user', '');

                if ($email_user == '')
                    return false;

                // Get title of send mail
                $title_email = $this->_request->getParam('title_email', '');

                if (empty ($title_email)) {

                    return false;
                }

                //Set parrams email sender
                $sender_email = $configsender['message']['email'];

                // Get email of the reciever
                $reciever_mail = $this->_request->getParam('receiver_mail', '');

                // Get message mail
                $message_mail = $this->_request->getParam('message_mail', '');

                $body_message = '<strong>'. $sender_name .'('. $email_user .')</strong><br/>Đã gửi cho bạn bài báo tại link: ' . $url_send;

                if (!empty ($message_mail))
                {
                    $body_message .= '<br/>Với nội dung </br>'. $message_mail;
                }

                $body_message .= '<hr><br/>Email này được gửi bằng tiện ích "Gửi cho bạn bè" của <b>thethao.VnExpress.net<b>';

                if (empty ($reciever_mail)) return false;

                $arrEmail = explode(';', $reciever_mail);
                
                $arrEmailReciever = array();
                if (!empty($arrEmail))
                {
                    foreach($arrEmail as $email)
                    {
                        $email = trim($email);

                        //If email is NOT valid
                        if(@!Fpt_Valid::isEmail($email))                        
                            continue;
                        
                        //Put data to arrReciever
                        $arrEmailReciever[] = array(
                            'email' => $email,
                            'alias' => $email
                        );
                    }
                    
                    if (!empty ($arrEmailReciever))
                    {
                        //Init options to send
                        $params = array(
                            'sender'    => $sender_email,
                            'name'      => $configsender['message']['name'],
                            'reciever'  => $arrEmailReciever,
                            'subject'   => $title_email,
                            'body'      => $body_message
                        );

                        // Set param for job
                        $jobParams = array();
                        $jobParams['class']     = 'JobMessage';
                        $jobParams['function']  = 'sendToEmail';
                        $jobParams['args']      = $params;

                        //Create job client
                        $jobClient = Fpt_Global::getJobClientInstance('sport');

                        //Register job
                        $jobClient->doBackgroundTask(Fpt_Global::getJobFunction('sport','send_mail'), $jobParams);
                    }
                    else {
                        $response['error'] = 1;
                        $response['message'] = 'Email gửi đến không hợp lệ';
                    }
                }
                
            }
            else {
                $response['error'] = 1;
                $response['message'] .= 'Empty recipient';
            }
        }
        catch (Exception $ex)
        {
            // Log error
            Fpt_Log::sendlog(4, $ex->getCode(), $ex->getMessage(), $ex->getTraceAsString(), null, null);
        }
                    
        echo Zend_Json::encode($response);
    }
}